package com.kayt.cyberBar.cuser.business.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.ServerResponse;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cuser.business.entity.po.BShexiangtou;
import com.kayt.cyberBar.cuser.business.service.BShexiangtouService;

@Service("shexiangtouService")
public class BShexiangtouServiceImpl implements BShexiangtouService {

	@Autowired
	private EntityDao entityDao;

	@Override
	public ServerResponse<Map<String, List<BShexiangtou>>> getData(String areal, String name, String channelId,String storeCode) {
		String hql = "select o from BShexiangtou o where 1 = 1";
		if (StringUtils.isNotEmpty(areal))
			hql += " and o.aeral = '" + areal + "'";
		if (StringUtils.isNotEmpty(name))
			hql += " and o.name = '" + name + "'";
		if (StringUtils.isNotEmpty(channelId))
			hql += " and o.channelId = '" + channelId + "'";
		if (StringUtils.isNotEmpty(storeCode))
			hql += " and o.storeCode = '" + storeCode + "'";
		hql += "order by o.seq asc";
		List<BShexiangtou> dataList = null;
		try {
			dataList = entityDao.getDataByHql(-1, -1, hql, new Object[] {});
		} catch (SQLException e) {
			e.printStackTrace();
			return ServerResponse.createByError();
		}
		Map<String, List<BShexiangtou>> result = new HashMap<>();
		if (dataList == null)
			return ServerResponse.createByError();
		for (BShexiangtou shexiangtou : dataList) {
			List<BShexiangtou> list = result.get(shexiangtou.getAeral());
			if (list == null) {
				list = new ArrayList<>();
			}
			list.add(shexiangtou);
			result.put(shexiangtou.getAeral(), list);
		}
		return ServerResponse.createBySuccess(result);
	}
}
